import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router';
import Login from '../views/Login/login.vue';
import home from '../views/Home/index.vue';
import userhome from '../views/User/index.vue';
import community from '../views/Community/index.vue';
import { ElMessage, ElMessageBox, ElNotification } from 'element-plus';
import article from '../views/articles/index.vue'
import ForYou from '../views/ForYou/index.vue'
import Come from '../views/Come/index.vue'
import draftsShow from '../views/User/draw/draftsShow.vue'
import Together from '../views/Together/index.vue';
import About from '../views/About/index.vue'
import AllArticles from '../views/ForYou/components/allArticles.vue';
import articleDetails from '../views/ForYou/components/articleditials.vue'
import userDetails from '../components/userDetails.vue'
import deepseek from '../views/articles/component/chatDeepseek.vue'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/home',
      name: 'home',
      component: home,
      children: [
        {
          path: '/userhome',
          component: userhome
        },
        {
          path: '/community',
          component: community
        },
        {
          path:'/article',
          component:article
        },
        {
          path:'/ForYou',
          component:ForYou
        },
        {
          path:'/Come',
          component:Come
        },
        {
          path:'/draftsShow',
          component:draftsShow
        },
        {
          path:'/together',
          component:Together
        },
        {
          path:'/About',
          component:About
        },
        {
          path:'/allArticle',
          component:AllArticles
        },
        {
          path:'/articleDetails',
          component:articleDetails
        },
        {
          path:'/userDetails',
          component:userDetails
        },
        {
          path:'/deepseek',
          component:deepseek
        }
      ]
    }
  ]
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  const tokenExpiry = localStorage.getItem('tokenExpiry');
  const currentTime = new Date().getTime();
  
  if (token && tokenExpiry && currentTime <= parseInt(tokenExpiry)) {

    // Token 存在且未过期
    if (to.path === '/login' || to.path === '/' ) {
      // 如果已登录，访问登录页时跳转到首页
      ElNotification({
        title: '欢迎回来！',
        message: '您已经登录',
        type: 'success',
        showClose: false,
      });
      next('/ForYou');
    } else {
      next(); // 允许访问目标页面
    }
  } else {
    // Token 不存在或已过期
    if (to.path !== '/login') {
      // 非登录页面，跳转到登录页
      localStorage.removeItem("username")
      localStorage.removeItem("token")
      localStorage.removeItem("tokenExpiry")
      ElNotification({
        title: '登录',
        message: '您还没有登录,请先登录享受所有服务',
        type: 'error',
        showClose: false,
      });
      next('/login');
    } else {
      localStorage.removeItem("username")
      localStorage.removeItem("token")
      localStorage.removeItem("tokenExpiry")
      ElNotification({
        title: '登录',
        message: '您还没有登录,请先登录享受所有服务',
        type: 'error',
        showClose: false,
      });
      next(); // 访问登录页
    }
  }
});

export default router;
